package com.car.loan.project.xr.controller;

import com.car.loan.common.exception.loan.LoanParameterException;
import com.car.loan.framework.aspectj.lang.annotation.DataStatus;
import com.car.loan.framework.aspectj.lang.annotation.Log;
import com.car.loan.framework.aspectj.lang.enums.BusinessType;
import com.car.loan.framework.web.domain.AjaxResult;
import com.car.loan.project.xr.domain.LoanStatus;
import com.car.loan.project.xr.service.ILoanService;
import com.car.loan.project.xr.service.ILoanStatusService;
import com.car.loan.project.xr.service.impl.LoanMakeLoansService;
import com.car.loan.project.xr.service.impl.RepaymentInfoService;
import com.car.loan.project.xr.util.bo.business.LoanBaseInfo;
import com.car.loan.project.xr.util.bo.business.RepaymentInfoBo;
import com.car.loan.project.xr.util.enums.FileAuditStatus;
import com.car.loan.project.xr.util.enums.RepaymentInfoStatus;
import com.car.loan.project.xr.util.repayment.RepaymentInfo;
import com.car.loan.project.xr.util.select.LoanBasePageParam;
import com.car.loan.project.xr.util.vo.LoanMakeLoansDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.Optional;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author huangding
 * @description
 * @date 2019/12/8 10:33
 */
@RestController
@RequestMapping("xr/repayment/info")
@Api(value = "还款信息", tags = "车贷业务-还款信息")
public class RepaymentInfoController {

    @Autowired
    private ILoanService loanService;

    @Autowired
    private ILoanStatusService loanStatusService;

    @Autowired
    private RepaymentInfoService repaymentInfoService;

    @Autowired
    private LoanMakeLoansService loanMakeLoansService;

    @PreAuthorize("@ss.hasPermi('repayment:info:list')")
    @GetMapping("list")
    @ApiOperation(value = "获取还款信息列表", response = LoanBaseInfo.class, responseContainer = "list")
    @DataStatus(loanStatusEnum = com.car.loan.common.enums.LoanStatusEnum.REPAYMENT_INFO_STATUS)
    public AjaxResult applyList(@Valid LoanBasePageParam loanBasePageParam) {
        return AjaxResult.success(loanService.page(loanBasePageParam));
    }


    @PreAuthorize("@ss.hasPermi('repayment:info:query')")
    @GetMapping("{loanId}")
    @ApiOperation(value = "获取还款信息详情", response = LoanMakeLoansDetailVo.class)
    public AjaxResult saveApply(@PathVariable Long loanId) {
        return AjaxResult.success(loanMakeLoansService.detail(loanId));
    }

    @PreAuthorize("@ss.hasPermi('repayment:info:query')")
    @GetMapping("getRepaymentInfo")
    @ApiOperation(value = "获取还款数据", notes = "首次账单日，(首)月还款额，当分期金额大于融资金额不会返回(首)月还款", response = RepaymentInfo.class)
    public AjaxResult getRepaymentInfo(@ApiParam("业务id") @RequestParam("loanId") Long loanId,
        @ApiParam("放贷时间") @RequestParam("lendDate") String lendDate,
        @ApiParam("账单日") @RequestParam("statementDay") Integer statementDay
    ) {
        return repaymentInfoService.getRepaymentInfo(loanId, lendDate, statementDay);
    }


    @PreAuthorize("@ss.hasPermi('repayment:info:input')")
    @PostMapping("input")
    @Log(title = "还款信息录入", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "还款信息录入")
    public AjaxResult audit(@RequestBody @Valid RepaymentInfoBo repaymentInfoBo) {
        return repaymentInfoService.input(repaymentInfoBo);
    }

    @PreAuthorize("@ss.hasPermi('repayment:info:withdraw')")
    @PutMapping("withdraw/{loanId}")
    @Log(title = "撤回还款信息录入", businessType = BusinessType.WITHDRAW)
    @ApiOperation(value = "撤回还款信息录入", notes = "档案审核后不可撤回")
    public AjaxResult withdrawApply(@PathVariable Long loanId) {
        Optional<LoanStatus> optionalLoanStatus = loanStatusService.getByLoanId(loanId);
        LoanStatus loanStatus = optionalLoanStatus
            .orElseThrow(() -> new LoanParameterException("业务状态数据异常请联系管理员"));
        Long fileAuditStatus = loanStatus.getFileAuditStatus();
        if (FileAuditStatus.SUBMIT.status == fileAuditStatus) {
            return AjaxResult.error("档案审核后不可撤回，不可撤回");
        }
        loanStatus.setRepaymentInfoStatus((long) RepaymentInfoStatus.INIT.status);
        loanStatusService.updateLoanStatus(loanStatus);
        return AjaxResult.success();
    }
}
